//
//  MyResumeManageViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/8.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "MyResumeManageViewController.h"
#import "MyResumeManageCell.h"
#import "UIButton+zt_adjustImageAndTitle.h"
#import "AddResumeViewController.h"
#import "EditResumeAllViewController.h"
#import "PreviewResumeViewController.h"
#import "UIButton+EdgeInsets.h"

@interface MyResumeManageViewController ()<UITableViewDelegate, UITableViewDataSource, MyResumeManageCellDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *resumeArr;

@end

@implementation MyResumeManageViewController

- (void)viewWillAppear:(BOOL)animated
{
	[self getResumeInfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
    // Do any additional setup after loading the view.
	[self createUI];
	[self refreshData];
}

- (void)createUI {
	
	self.navigationItem.title = @"我的简历";
	self.view.backgroundColor = UISeparatedColorRGB;
	_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-50-20) style:UITableViewStylePlain];
	_tableView.delegate = self;
	_tableView.dataSource = self;
	[_tableView registerNib:[UINib nibWithNibName:@"MyResumeManageCell" bundle:nil] forCellReuseIdentifier:@"cell"];
	_tableView.backgroundColor = UISeparatedColorRGB;
	_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.view addSubview:_tableView];
	
	UIButton *createNewBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, _tableView.height+20, SCREEN_WIDTH, 50)];
	[createNewBtn setBackgroundColor:UIMainColorRGB];
	[createNewBtn addTarget:self action:@selector(addNewResume:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:createNewBtn];
	[self.view bringSubviewToFront:createNewBtn];
	UIButton *placeHolderBtn = [[UIButton alloc] initWithFrame:CGRectMake(SW(2)-SW(4), 0, SW(2), 50)];
	[placeHolderBtn setImage:IMAGE(@"icon_add_resume") forState:UIControlStateNormal];
	[placeHolderBtn setTitle:@"创建新简历" forState:UIControlStateNormal];
	placeHolderBtn.userInteractionEnabled = NO;
	[placeHolderBtn layoutButtonWithEdgeInsetsStyle:ButtonEdgeInsetsStyleImageLeft imageTitlespace:5];
	[createNewBtn addSubview:placeHolderBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)refreshData {
	__weak typeof (self) weakSelf = self;
	
	_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
		NSLog(@"下拉刷新");
		[weakSelf getResumeInfo];
	}];
	
}

//TableView delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	MyResumeManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
	cell.title.text = self.resumeArr[indexPath.row][@"title"];
	cell.salary.text = self.resumeArr[indexPath.row][@"salary"];
	cell.delegate = self;
	cell.resumeId = self.resumeArr[indexPath.row][@"resume_id"];
	NSString *isDefault = self.resumeArr[indexPath.row][@"is_default"];
	if ([isDefault isEqualToString:@"1"]) {
		cell.defaultBtn.selected = YES;
	}
	else {
		cell.defaultBtn.selected = NO;
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	
	
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [self.resumeArr count];
	
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 120;
}

- (NSMutableArray *)resumeArr
{
	if (!_resumeArr) {
		_resumeArr = [[NSMutableArray alloc] init];
	}
	return _resumeArr;
}

- (void)getResumeInfo
{
	[[NetworkTool sharedTool] requestWithName:@"获取简历列表" URLString:APIResumeManage parameters:nil method:@"POST" callBack:^(id responseObject) {
		
		[self.resumeArr removeAllObjects];
		NSArray *arr = responseObject[@"data"];
		for (NSDictionary *dict in arr){
			[self.resumeArr addObject:dict];
		}
		dispatch_async(dispatch_get_main_queue(), ^{
			[MBProgressHUD hideHUDForView:self.view animated:YES];
			[self.tableView.mj_header endRefreshing];
			[self.tableView reloadData];
		});
	}];
}

- (void)addNewResume:(UIButton *)sender
{
	AddResumeViewController *addResumeVC = [[AddResumeViewController alloc] init];
	[self presentViewController:addResumeVC animated:YES completion:nil];
}


- (void)editResume:(UIButton *)button :(NSString *)resumeId
{
	EditResumeAllViewController *vc = [[EditResumeAllViewController alloc] init];
	vc.resumeId = resumeId;
	[self.navigationController pushViewController:vc animated:YES];
}

- (void)deleteResume:(UIButton *)button :(NSString *)resumeId
{
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定删除？" message:nil preferredStyle:UIAlertControllerStyleAlert];
	[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		[[NetworkTool sharedTool] requestWithName:@"删除简历" URLString:APIDeleteResume parameters:@{@"resume_id":resumeId} method:@"POST" callBack:^(id responseObject) {
			//NSString *message = responseObject[@"data"][@"message"];
			NSString *message = @"删除成功";
			[self presentViewController:[LZCTools alertMessage:message] animated:YES completion:nil];
			[self getResumeInfo];
		}];
	}]];
	[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
	}]];
	[self presentViewController:alert animated:YES completion:nil];
}

- (void)previewResume:(UIButton *)button :(NSString *)resumeId
{
	PreviewResumeViewController *previewVC = [[PreviewResumeViewController alloc] init];
	previewVC.resumeId = resumeId;
	[self.navigationController pushViewController:previewVC animated:YES];
}

- (void)setDefaultResume:(UIButton *)button :(NSString *)resumeId
{
	[[NetworkTool sharedTool] requestWithName:@"设置默认简历" URLString:APISetDefaultResume parameters:@{@"resume_id":resumeId} method:@"POST" callBack:^(id responseObject) {
		[self getResumeInfo];
	}];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
